/**
 * 2013-6-21 下午9:31:53 Created By niexiaoqiang
 */

package com.xiaoqiang.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 获取用户网络类型
 */
public class GetUserNetWorkType {

	private static final int NOTWIFI = 0;
	private static final int WIFI = 1;
	private static Logger Log = new Logger(GetUserNetWorkType.class);

	/**
	 * 获取当前的网络状态 -1：没有网络 1：WIFI网络;0 非wifi网络
	 * 
	 * @param context
	 * @return
	 */
	@SuppressLint("DefaultLocale")
	public static int getAPNType(Context context) {
		int netType = -1;
		NetworkInfo networkInfo = getNetworkInfo(context);
		if (networkInfo == null) {
			Log.debug("there is nothing network");
			return netType;
		}
		int nType = networkInfo.getType();
		if (nType == ConnectivityManager.TYPE_MOBILE) {
			Log.debug("networkInfo.getExtraInfo() is " + networkInfo.getExtraInfo());
			netType = NOTWIFI;
		} else if (nType == ConnectivityManager.TYPE_WIFI) {
			netType = WIFI;
		}
		return netType;
	}

	/**
	 * @function 得到网络信息
	 * @param context
	 * @return
	 */
	public static NetworkInfo getNetworkInfo(Context context) {
		ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		return connMgr.getActiveNetworkInfo();
	}
}
